﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService

Partial Class Control_MSDS_Section03
	Inherits System.Web.UI.UserControl
	Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
	Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Translate()
        If Not IsPostBack Then
            Me.EnableViewState = False
            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "duyjs", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "duyjs", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If
            If Not Request.QueryString("Id").ToString.Equals("0") Then
                hidMSDSID.Value = Request.QueryString("Id")
                btnPrevious.Attributes.Add("onclick", "location.href='Section02.aspx?Id=" & hidMSDSID.Value & "'")
            End If
            CheckData()
        End If
	End Sub
	Sub CheckData()
		Using data As New VSEntities
			Dim LanguageId = Session("LanguageId").ToString()
			Dim p = (From q In data.MSDSSection03
			   Where q.LanguageId = LanguageId And q.MSDSId = hidMSDSID.Value
			   ).FirstOrDefault()
			If Not p Is Nothing Then
				txtS311.Text = p.S311
				txtS321.Text = p.S321
				txtS331.Text = p.S331
				hidID.Value = p.Section03Id
			End If
		End Using
	End Sub
#End Region
#Region "Event for control "
	Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
		Using data As New VSEntities
			Try
				Dim p As MSDSSection03
				p = (From q In data.MSDSSection03
				Where q.Section03Id = hidID.Value
				  ).FirstOrDefault()
				Dim _isNew = True
				If Not p Is Nothing Then
					_isNew = False
				Else
					p = New MSDSSection03
				End If
				With p
					.S311 = txtS311.Text.Trim
					.S321 = txtS321.Text.Trim
					.S331 = txtS331.Text.Trim
					.LanguageId = Session("LanguageId")
					.MSDSId = hidMSDSID.Value
				End With
				If _isNew Then
					p.Created = Now
					p.Creator = Session("UserName")
					data.MSDSSection03.AddObject(p)
				Else
					p.Modified = Now
					p.Modifier = Session("UserName")
				End If
				data.SaveChanges()

				hidID.Value = p.Section03Id
				'Delete all by Section03Id
				Dim _sqlString As String = " DELETE FROM SECTION03Detail WHERE Section03ID=" & hidID.Value
				data.ExecuteStoreCommand(_sqlString)
				'' Insert Section03Detail
				If hidValue.Value <> "" Then
					Dim listData = hidValue.Value.Split("$")
					For index = 0 To listData.Count - 1
						Dim _p As New Section03Detail
						If listData(index) <> "" Then
							Dim _lst = listData(index).Split("#")
							If _lst(0) <> "" And _lst(1) <> "" Then
								_p.Section03Id = hidID.Value
								_p.TypeOtherListTable = _lst(0)
								_p.IdOtherList = _lst(1)
								data.Section03Detail.AddObject(_p)
							End If
						End If
					Next
					data.SaveChanges()
				End If
				Insert_App_Log(If(_isNew, "Insert", "Update") & "  MSDS Section03:" & hidMSDSID.Value & "", Function_Name.MSDSOriginal, If(_isNew, Audit_Type.Create, Audit_Type.Edit), Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
				Excute_Javascript("AlertboxRedirect('Lưu dữ liệu thành công.','../../Page/MSDS/Section04.aspx?Id=" & hidMSDSID.Value & "');", Me.Page, True)

			Catch ex As Exception
				log4net.Config.XmlConfigurator.Configure()
				log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
				Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
			Finally
				GC.Collect()
			End Try
		End Using
	End Sub
	Protected Sub Translate()

        'ltrRequired.Text = getTranslate("ltrRequired", Session("LanguageId"), ltrRequired.Text)
		lbSection03Title.Text = getTranslate("lbSection03Title", Session("LanguageId"), lbSection03Title.Text)
		lblSection03MucXepLoai.Text = getTranslate("lblSection03MucXepLoai", Session("LanguageId"), lblSection03MucXepLoai.Text)
		lblSection03CanhBaoNH.Text = getTranslate("lblSection03CanhBaoNH", Session("LanguageId"), lblSection03CanhBaoNH.Text)
		lblSection03CacDuongTiepXuc.Text = getTranslate("lblSection03CacDuongTiepXuc", Session("LanguageId"), lblSection03CacDuongTiepXuc.Text)
		ltrNguyHaiVatChat.Text = getTranslate("ltrNguyHaiVatChat", Session("LanguageId"), ltrNguyHaiVatChat.Text)

		ltrSection03TTNH0.Text = getTranslate("ltrSection03TTNH", Session("LanguageId"), ltrSection03TTNH0.Text)
		ltrSection03PL0.Text = getTranslate("ltrSection03PL", Session("LanguageId"), ltrSection03PL0.Text)
		ltrSection03CT0.Text = getTranslate("ltrSection03CT", Session("LanguageId"), ltrSection03CT0.Text)

		ltrSection03TTNH1.Text = ltrSection03TTNH0.Text ' getTranslate("ltrSection03TTNH", Session("LanguageId"), ltrSection03TTNH1.Text)
		ltrSection03PL1.Text = ltrSection03PL0.Text 'getTranslate("ltrSection03PL", Session("LanguageId"), ltrSection03PL1.Text)
		ltrSection03CT1.Text = ltrSection03CT0.Text 'getTranslate("ltrSection03CT", Session("LanguageId"), ltrSection03CT1.Text)

		ltrSection03TTNH2.Text = ltrSection03TTNH0.Text 'getTranslate("ltrSection03TTNH", Session("LanguageId"), ltrSection03TTNH2.Text)
		ltrSection03PL2.Text = ltrSection03PL0.Text ' getTranslate("ltrSection03PL", Session("LanguageId"), ltrSection03PL2.Text)
		ltrSection03CT2.Text = ltrSection03CT0.Text 'getTranslate("ltrSection03CT", Session("LanguageId"), ltrSection03CT2.Text)

		ltrNguyHaiSucKhoe.Text = getTranslate("ltrNguyHaiSucKhoe", Session("LanguageId"), ltrNguyHaiSucKhoe.Text)
		ltrNguyHaiMoiTruong.Text = getTranslate("ltrNguyHaiMoiTruong", Session("LanguageId"), ltrNguyHaiMoiTruong.Text)
		btnAdd1.Value = getTranslate("btnAddSection03", Session("LanguageId"), btnAdd1.Value)
		btnAdd2.Value = btnAdd1.Value 'getTranslate("btnAddSection03", Session("LanguageId"), btnAdd2.Value)
		btnAdd3.Value = btnAdd1.Value 'getTranslate("btnAddSection03", Session("LanguageId"), btnAdd3.Value)
		hidOption.Value = getTranslate("strSelect", Session("LanguageId"), hidOption.Value)
		hidStringExists.Value = getTranslate("strExistsSection03", Session("LanguageId"), hidStringExists.Value)
		hidDel.Value = getTranslate("strDelete", Session("LanguageId"), hidDel.Value)
	End Sub

#End Region
End Class
